package cn.jeeweb.web.modules.equipment.service.impl;

import cn.jeeweb.common.mybatis.mvc.service.impl.CommonServiceImpl;
import cn.jeeweb.common.utils.StringUtils;
import cn.jeeweb.web.modules.equipment.entity.EqsMaintenanceCheckTemplate;
import cn.jeeweb.web.modules.equipment.mapper.EqsMaintenanceCheckTemplateMapper;
import cn.jeeweb.web.modules.equipment.service.IEqsMaintenanceCheckTemplateService;
import cn.jeeweb.web.modules.sys.entity.FileResponse;
import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
* @description: eqs_maintenance_check服务接口
*/
@Transactional
@Service("eqsMaintenanceCheckTemplateService")
public class EqsMaintenanceCheckTemplateServiceImpl extends CommonServiceImpl<EqsMaintenanceCheckTemplateMapper,EqsMaintenanceCheckTemplate> implements IEqsMaintenanceCheckTemplateService {


    public EqsMaintenanceCheckTemplate insertAndSelect(String json){
        EqsMaintenanceCheckTemplate eqsMaintenanceCheckTemplate = JSON.parseObject(json,EqsMaintenanceCheckTemplate.class);
        eqsMaintenanceCheckTemplate = initEqsMaintenanceCheckTemplate(eqsMaintenanceCheckTemplate);
        return insertAndSelect(eqsMaintenanceCheckTemplate);
    }

    public boolean insertOrUpdate(String json){
        EqsMaintenanceCheckTemplate eqsMaintenanceCheckTemplate = JSON.parseObject(json,EqsMaintenanceCheckTemplate.class);
        eqsMaintenanceCheckTemplate = initEqsMaintenanceCheckTemplate(eqsMaintenanceCheckTemplate);
        return insertOrUpdate(eqsMaintenanceCheckTemplate);
    }

    public EqsMaintenanceCheckTemplate getById(String json){
        EqsMaintenanceCheckTemplate result = super.getById(json);
        if (result != null) {
            List<FileResponse> defaultList = new ArrayList<FileResponse>();
            if (StringUtils.isNotBlank(result.getCheckAreaImage())) {
                List<FileResponse> responses = JSON.parseArray(result.getCheckAreaImage(), FileResponse.class);
                result.setCheckAreaImageArray(responses);
            } else {
                result.setCheckAreaImageArray(defaultList);
            }

            if (StringUtils.isNotBlank(result.getCheckImportImage())) {
                List<FileResponse> responses = JSON.parseArray(result.getCheckImportImage(), FileResponse.class);
                result.setCheckImportImageArray(responses);
            } else {
                result.setCheckImportImageArray(defaultList);
            }

            if (StringUtils.isNotBlank(result.getUseToolFile())) {
                List<FileResponse> responses = JSON.parseArray(result.getUseToolFile(), FileResponse.class);
                result.setUseToolFileArray(responses);
            } else {
                result.setUseToolFileArray(defaultList);
            }

            if (StringUtils.isNotBlank(result.getCheckStandardFile())) {
                List<FileResponse> responses = JSON.parseArray(result.getCheckStandardFile(), FileResponse.class);
                result.setCheckStandardFileArray(responses);
            } else {
                result.setCheckStandardFileArray(defaultList);
            }

            if (StringUtils.isNotBlank(result.getCheckMethodFile())) {
                List<FileResponse> responses = JSON.parseArray(result.getCheckMethodFile(), FileResponse.class);
                result.setCheckMethodFileArray(responses);
            } else {
                result.setCheckMethodFileArray(defaultList);
            }

            if (StringUtils.isNotBlank(result.getExistRiskFile())) {
                List<FileResponse> responses = JSON.parseArray(result.getExistRiskFile(), FileResponse.class);
                result.setExistRiskFileArray(responses);
            } else {
                result.setExistRiskFileArray(defaultList);
            }

            if (StringUtils.isNotBlank(result.getCountermeasuresFile())) {
                List<FileResponse> responses = JSON.parseArray(result.getCountermeasuresFile(), FileResponse.class);
                result.setCountermeasuresFileArray(responses);
            } else {
                result.setCountermeasuresFileArray(defaultList);
            }
        }
        return result;
    }

    @Override
    public List<EqsMaintenanceCheckTemplate> getEqsMaintenanceCheckTemplate(String[] ids) {
        return baseMapper.getEqsMaintenanceCheckTemplate(ids);
    }

    @Override
    public List<EqsMaintenanceCheckTemplate> getCheckTemplateByPlanId(String planId) {
        return baseMapper.getCheckTemplateByPlanId(planId);
    }


    public EqsMaintenanceCheckTemplate initEqsMaintenanceCheckTemplate(EqsMaintenanceCheckTemplate eqsMaintenanceCheckTemplate){
        List<FileResponse> areaImages = eqsMaintenanceCheckTemplate.getCheckAreaImageArray();
        List<FileResponse> importImages = eqsMaintenanceCheckTemplate.getCheckImportImageArray();
        List<FileResponse> useToolFiles = eqsMaintenanceCheckTemplate.getUseToolFileArray();
        List<FileResponse> standardFiles = eqsMaintenanceCheckTemplate.getCheckStandardFileArray();
        List<FileResponse> methodFiles = eqsMaintenanceCheckTemplate.getCheckMethodFileArray();
        List<FileResponse> existRiskFiles = eqsMaintenanceCheckTemplate.getExistRiskFileArray();
        List<FileResponse> countermeasuresFiles = eqsMaintenanceCheckTemplate.getCountermeasuresFileArray();
        String areaImage = getResult_(areaImages);
        String importImage = getResult_(importImages);
        String useToolFile = getResult_(useToolFiles);
        String standardFile = getResult_(standardFiles);
        String methodFile = getResult_(methodFiles);
        String existRiskFile = getResult_(existRiskFiles);
        String countermeasuresFile = getResult_(countermeasuresFiles);

        eqsMaintenanceCheckTemplate.setCheckAreaImage(areaImage);
        eqsMaintenanceCheckTemplate.setCheckImportImage(importImage);
        eqsMaintenanceCheckTemplate.setUseToolFile(useToolFile);
        eqsMaintenanceCheckTemplate.setCheckStandardFile(standardFile);
        eqsMaintenanceCheckTemplate.setCheckMethodFile(methodFile);
        eqsMaintenanceCheckTemplate.setExistRiskFile(existRiskFile);
        eqsMaintenanceCheckTemplate.setCountermeasuresFile(countermeasuresFile);
        return eqsMaintenanceCheckTemplate;
    }

    public String getResult_(List<FileResponse> list){
        String result = "";
        if(list != null && list.size() > 0){
            result = JSON.toJSONString(list);
        }
        return result;
    }

}